/**
 * @author LKQ
 * @date 2022/2/18 13:56
 * @description
 */
public class FormalSolution {
    public static void main(String[] args) {
        FormalSolution formalSolution = new FormalSolution();
        int[] nums = {2, 6, 4, 8, 10, 9 , 15};
        formalSolution.findUnsortedSubarray(nums);
    }
    public int findUnsortedSubarray(int[] nums) {
        int n = nums.length;
        int maxn = Integer.MIN_VALUE, right = -1;
        int minn = Integer.MAX_VALUE, left = -1;
        for (int i = 0; i < n; i++) {
            if (maxn > nums[i]) {
                // 找到最后一个比最大值小的位置
                right = i;
            } else {
                maxn = nums[i];
            }
            if (minn < nums[n - i - 1]) {
                left = n - i - 1;
            } else {
                minn = nums[n - i - 1];
            }
        }
        return right == -1 ? 0 : right - left + 1;
    }
}
